package com.kingtown.inventory.activity;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kingtown.inventory.MainApplication;
import com.kingtown.inventory.R;
import com.kingtown.inventory.Utils;
import com.kingtown.inventory.domain.bo.LoginBo;
import com.kingtown.inventory.domain.dto.AssetDto;
import com.kingtown.inventory.domain.so.AssetSo;
import com.kingtown.inventory.net.NetWorkManager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    MainApplication mApplication;
    Button btnLogin;
    Context _this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();// 隐藏ActionBar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);
        _this = this;
        mApplication = (MainApplication) this.getApplication();
        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                LoginBo bo = new LoginBo();
                bo.setUsername("super");
                bo.setPassword("admin");
                NetWorkManager.request()
                        .login(bo)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(res -> {
                            if (res.isSuccess()) {
                                Log.i("res", res.toString());
                                JSONObject jsonObject = JSONUtil.parseObj(res.getData());
                                String accessToken = jsonObject.getStr("accessToken");
                                NetWorkManager.setToken(accessToken);
                                startActivity(new Intent().setClass(_this,MainActivity.class));
                            } else {
                                Toast.makeText(_this, "请求失败", Toast.LENGTH_SHORT).show();
                            }
                        }, throwable -> {
                            Toast.makeText(_this, "请求失败：" + throwable + "！", Toast.LENGTH_SHORT).show();
                            Utils.hideProgressDialog();
                        });



                break;
        }
    }
}